#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
from logging.handlers import RotatingFileHandler

from flask import Flask

from webapp.config import config


def create_app(config_name):
    app = Flask(__name__, instance_relative_config=True)
    # 加载配置
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    # app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 360000

    # 记录日志
    handler = RotatingFileHandler('app.log', maxBytes=10000, backupCount=1)
    handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]'
    ))
    handler.setLevel(logging.INFO)
    app.logger.addHandler(handler)
    return app
